package cc.rengu.igas.smps.core.realize.impl;

import cc.rengu.igas.share.common.util.GlobalEsbUtil;
import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.core.realize.SmpsChannelPublicDealService;
import cc.rengu.igas.smps.facade.base.Header;
import cc.rengu.igas.smps.facade.request.AlarmNoticeRequest;
import cc.rengu.igas.smps.facade.response.AlarmNoticeResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class SmpsChannelPublicDealServiceImpl implements SmpsChannelPublicDealService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public void pushRiskMessage(String riskMessage, String alaramLevel, String respCode, String txnNum, String txnName) {
        try {
            DubboService dubboService = new DubboServiceImpl();
            AlarmNoticeRequest alarmNoticeRequest = new AlarmNoticeRequest();
            Header header = new Header();
            ConvertUtil.convertOutput(header);
            header.setTxnNum("0077000000RW01");
            header.setBizType(AppParamConstant.DEFAULT_BIZ_TYPE);
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setTraceNo(GlobalEsbUtil.generateEsbSeqeuenceNumber(header.getInstId()));
            alarmNoticeRequest.setHeader(header);
            alarmNoticeRequest.setAlarmLevel(alaramLevel);
            alarmNoticeRequest.setTransCode(txnNum);
            alarmNoticeRequest.setTransName(txnName);
            alarmNoticeRequest.setErrorCode(respCode);
            alarmNoticeRequest.setAlarmDesc(riskMessage);
            alarmNoticeRequest.setAlarmInfo(riskMessage);
            DubboResult<AlarmNoticeResponse> result = dubboService.callAtomService(alarmNoticeRequest, SmpsServiceConstant.ALARM_NOTICE_SERVICE, AlarmNoticeResponse.class);
            if (null == result || null == result.getResult() || !"00000000".equals(result.getResult().getRespCode())) {
                rglog.error("告警平台调用失败！");
            } else {
                rglog.info("告警平台调用成功！");
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("调用告警平台异常:<{}>", byteArrayOutputStream.toString());
        }
    }
}
